<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-row>
        <el-col :span="15">
          <el-form-item label="名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="链接" prop="url">
            <el-input v-model="dataForm.url" placeholder="URL"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="logo:" prop="logo">
            <div class="myhead">
              <el-tooltip content="点击上logo" placement="top">
                <el-image  style="width: 90px; height: 60px" :src="dataForm.logo" fit="contain" @click="uploadImageHandle()" alt=""/>
              </el-tooltip>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="顺序" prop="sort">
            <el-input v-model="dataForm.sort" placeholder="顺序"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="15">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    <!-- 弹窗, 上传缩略图 -->
    <upload-image v-if="uploadImageVisible" ref="uploadImage" @refreshDataList="refreshUploadImage"></upload-image>
  </el-dialog>
</template>

<script>
  // 上传缩略图
  import UploadImage from './upload-image-common'
  export default {
    data () {
      return {
        visible: false,
        uploadImageVisible: false,
        dataForm: {
          id: 0,
          name: '',
          url: '',
          logo: 'http://file-cdn-theme.cykonhy.com/default/default.jpg',
          sort: '',
          remark: '',
        },
        dataRule: {
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: 'URL不能为空', trigger: 'blur' }
          ],
          logo: [
            { required: true, message: 'logo不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '顺序不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    components: {
      UploadImage
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/friendly-link/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.friendlyLink.name
                this.dataForm.url = data.friendlyLink.url
                this.dataForm.logo = data.friendlyLink.logo
                this.dataForm.sort = data.friendlyLink.sort
                this.dataForm.remark = data.friendlyLink.remark
              }
            })
          }
        })
      },
      // 上传图片
      uploadImageHandle () {
        this.uploadImageVisible = true
        this.$nextTick(() => {
          this.$refs.uploadImage.init()
        })
      },
      refreshUploadImage (filePath) {
        if (filePath != ""){
          this.dataForm.logo = filePath
        }
        this.uploadImageVisible = false
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/friendly-link/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'url': this.dataForm.url,
                'logo': this.dataForm.logo,
                'sort': this.dataForm.sort,
                'remark': this.dataForm.remark,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
